#define _CRT_SECURE_NO_WARNINGS 1
#include "LinkStack.h"
#include <stdio.h>
#include<stdlib.h>

void InitStack(Stack* s)
{
	s->ps = NULL;
	s->count = 0;
}

void DestroyStack(Stack* s)
{
	while (!StackEmpty(s))
	{
		ElemType e;
		Pop(s, &e);
	}
}

int StackEmpty(Stack* s)
{
	if (s->ps == NULL && s->count == 0) return 1;
	return 0;
}

void GetTop(Stack* s, ElemType* e)
{
	*e = s->ps->data;
}

Status Push(Stack* s, ElemType e)
{
	LinkStackPtr node = (LinkStackPtr)malloc(sizeof(Node));
	node->data = e;
	node->next = s->ps;
	s->ps = node; 
	s->count++;
	return OK;
}

Status Pop(Stack* s, ElemType* e)
{
	if (StackEmpty(s)) return ERROR;
	LinkStackPtr p = s->ps;
	s->ps = p->next;
	*e = p->data;
	free(p);
	s->count--;
	return OK;
}

int StackLength(Stack* s)
{
	return s->count;
}